package com.hp.techcoin14.mails;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.moyosoft.connector.com.ComponentObjectModelException;
import com.moyosoft.connector.exception.LibraryNotFoundException;
import com.moyosoft.connector.ms.outlook.Outlook;
import com.moyosoft.connector.ms.outlook.folder.FolderType;
import com.moyosoft.connector.ms.outlook.folder.OutlookFolder;
import com.moyosoft.connector.ms.outlook.item.ItemsIterator;
import com.moyosoft.connector.ms.outlook.item.OutlookItem;
import com.moyosoft.connector.ms.outlook.item.OutlookItemID;
import com.moyosoft.connector.ms.outlook.mail.OutlookMail;
import com.moyosoft.connector.ms.outlook.recipient.OutlookRecipient;

public class JOCOutlookMailProxy implements OutlookMailProxy {

	
	
	@Override
	public List<Mail> getAllMailsAfter(Date date) {

		List<Mail> mailes = new ArrayList<Mail>();
		
        ArrayList receivedIds = new ArrayList();
        try {
            // Outlook application
            Outlook outlookApplication = new Outlook();
            
            // Get the default inbox folder
            OutlookFolder inbox = outlookApplication
                    .getDefaultFolder(FolderType.INBOX);
            
            String filter = "[CreationTime] >= '" + formatDateToString(date) + "'";
            System.out.println(filter);
            
            ItemsIterator iterator = inbox.getItems().findItems(filter); 
            ArrayList foundIds = new ArrayList();

            while (iterator.hasNext()) { 
                OutlookItem item = iterator.nextItem();

                if(item != null && item.getType().isMail())
                {
                    OutlookMail mail = (OutlookMail) item;
                    OutlookItemID id = mail.getItemId();

                    if(!receivedIds.contains(id))
                    {
                    	mailes.add(handleNewMail(mail));
                    }

                    foundIds.add(id);
                }
            }

            receivedIds = foundIds;
            
            
        	outlookApplication.dispose();
        
        } catch(ComponentObjectModelException ex) {
            System.out.println("COM error has occured: ");
            ex.printStackTrace();
        } catch(LibraryNotFoundException ex) {
            // If this error occurs, verify the file 'moyocore.dll' is present
            // in java.library.path
            System.out.println("The Java Outlook Library has not been found.");
            ex.printStackTrace();
        } 
            
		return mailes;
	}
	
	/**
	 * see http://support.microsoft.com/default.aspx?scid=kb;EN-US;201081
	 * @param date
	 * @return
	 */
    private String formatDateToString(Date date) {
    	SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM d");
        return dateFormat.format(date);
    }
    
    private Mail handleNewMail(OutlookMail outlookMail) {
    	
    	Mail mail = new Mail();
    	mail.setSender(outlookMail.getSenderAddress());
    	
    	Iterator recipientsIterator = outlookMail.getRecipients().iterator();
    	
    	while (recipientsIterator.hasNext()) {
    		OutlookRecipient recipient = (OutlookRecipient) recipientsIterator.next();
    		mail.addRecipient(recipient.getAddress());
    	}

    	mail.setSubject(outlookMail.getSubject());
    	mail.setBody(outlookMail.getBody());
    	
    	return mail;
    }

}
